import { Routes, Route, Outlet } from "react-router-dom";
import Home from "@/components/home"
import AuthRouter from "@/routers/auth"
const ModulesFile = require.context('./', true, /index.js$/)
// 总路由集合
const RouterMap = []
ModulesFile.keys().map((item) => {
    //文件名
    const ModuleName = item.replace(/^.\/(.*)\.js/, '$1')
    //不包含当前index.js 文件
    if (ModuleName !== 'index') {
        //请求列表
        const ModuleList = ModulesFile(item)

        //追加到数组
        RouterMap.push(...ModuleList.default);
    }

})
const RouterList = () => {
    return (
            <Routes>
                <Route path="/" element={<Home />}></Route>
                <Route path="/" element={<><Outlet /></>}>
                    {
                        RouterMap.map((item, index) => {
                            return (
                                <Route key={index} path={item.path} element={<AuthRouter auth={item.auth} component={<item.component />}></AuthRouter>} >
                                    {
                                        item.children.map((child, childindex) => {
                                            return (
                                                <Route key={childindex} path={child.path} element={<AuthRouter auth={child.auth} component={<child.component />}></AuthRouter>}></Route>
                                            )
                                        })
                                    }
                                </Route>
                            )
                        })
                    }
                </Route>
            </Routes>
    )
}

export default RouterList 
